﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LTM_Extra;

namespace LTM_Main
{
    public partial class VariableCreation : Form
    {
        TableOfContentsWordCategoriesTemplates template;
        ContentProvider contentProvider;
        public VariableCreation(TableOfContentsWordCategoriesTemplates template, ContentProvider contentProvider)
        {
            InitializeComponent();
            this.template = template;
            this.contentProvider = contentProvider;
        }

        private void VariableCreation_Load(object sender, EventArgs e)
        {
            cmbVarType.DataSource = Constants.VariableType.GetList();
            Validator.ValidateEmpty(cmbVarType);
            Validator.ValidateEmpty(txtVarName);

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string initialValue = string.Empty;
            if (cmbInitialValues.DataSource != null)
            {
                foreach (var item in ((IEnumerable<string>)cmbInitialValues.DataSource))
                {
                    initialValue += item + Constants.InitialValueSeperator;
                }
            }


            Variable variable = new Variable()
            {
                VariableName = txtVarName.Text,
                VariableType = cmbVarType.SelectedValue.ToString(),
                DefaultValue = cmbInitialValues.SelectedValue == null ? string.Empty : cmbInitialValues.SelectedValue.ToString(),
                ErrorMessage = txtErrorMsg.Text,
                ValidationRule = txtValidationRule.Text,
                LableText = txtLabelName.Text,
                Required = ckbRequired.Checked,
                InitialValue = initialValue

            };
            contentProvider.AddTemplateVariable(template, variable);
            Close();
        }

        private void ckbCustom_CheckedChanged(object sender, EventArgs e)
        {

            txtValidationRule.Enabled = ckbCustom.Checked;
            txtErrorMsg.Enabled = ckbCustom.Checked;

        }

        private void btnInitialValue_Click(object sender, EventArgs e)
        {
            List<string> dataSource = new List<string>();
            StringCollectionEditor editor = new StringCollectionEditor(dataSource);
            editor.ShowDialog();
            cmbInitialValues.DataSource = dataSource;


        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
